package cn.catfish.controller;

import cn.catfish.entity.Book;
import cn.catfish.entity.BorrowRecord;
import cn.catfish.entity.User;
import cn.catfish.service.BookService;
import cn.catfish.service.RecordService;
import cn.catfish.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/book")
@Api(tags = "图书管理")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private RecordService recordService;


    @GetMapping("/getAllBooks")
    @ApiOperation(value = "查询所有图书")
    public List<Book> getAllBooks() {
        return bookService.findAllBooks();
    }
    @PostMapping("/insert")
    @ApiOperation(value = "添加图书")
    public void insertBook(Book book) {
        bookService.addBook(book);
    }

    @PostMapping("/borrow")
    @ApiOperation(value = "借书")
    public ResponseEntity<String> borrowBook(@RequestParam int id,@RequestParam String title) {
        User user = userService.getUserById(id);
        Book book = bookService.getBookName(title);
        if (user == null ) {
          return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
        }
        if (book == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("图书不存在");
        }
        boolean result = bookService.borrowBook(user, book);
        if (result == true) {
            userService.updateUser(user);
            bookService.updateBook(book);
            BorrowRecord br = new BorrowRecord();
            br.setUserId(user.getUserid());
            br.setBookId(book.getId());
            br.setBorrowDate(LocalDate.now());
            recordService.addRecord(br);
           return ResponseEntity.status(HttpStatus.OK).body("借书成功");
        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("借书失败");
        }

    }

    @PostMapping("/return")
    @ApiOperation(value = "还书")
    public ResponseEntity<String> returnBook(@RequestParam int id,@RequestParam String title) {
        User user = userService.getUserById(id);
        Book book = bookService.getBookName(title);
        if (user == null ) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
        }
        if (book == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("图书不存在");
        }
        List<BorrowRecord> br = recordService.getRecords(user.getUserid(), book.getId());
        if (br == null || br.isEmpty()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("没有借阅记录");
        }
        LocalDate now = LocalDate.now();
        LocalDate borrowDate = br.get(0).getBorrowDate();
        long overdueDays =  now.toEpochDay() - borrowDate.toEpochDay();
        boolean result1 = bookService.returnBook(user.getUserid(), book.getId(), overdueDays);
        if (result1!=false) {
            recordService.deleteRecord(br.get(0).getId());
            return ResponseEntity.status(HttpStatus.OK).body("还书成功");
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("还书失败");
    }

}
